//
//  LocationExt.swift
//  JiaoZiTaxi
//
//  Created by Hu Gang on 2022/10/11.
//

import Foundation
import MapKit

extension CLLocationCoordinate2D {
  func distance(to dest:CLLocationCoordinate2D) -> String {
    let coordinate₀ = CLLocation(latitude: self.latitude , longitude: self.longitude)
    let coordinate₁ = CLLocation(latitude: dest.latitude , longitude: dest.longitude)

    let distanceInMeters = Int(coordinate₀.distance(from: coordinate₁))

    let km = distanceInMeters / 1000
    let meters = distanceInMeters - km * 1000

    var s = ""
    if km > 0 {
      s = s + "\(km)公里"
    }

    if meters > 0 {
        s = s + "\(meters)\("米".localized())"
    }

    return s
  }
}
